﻿using System;
using System.ComponentModel.Composition;
using System.Xml.Serialization;
using Peter.Common;
using Peter.Common.Interfaces;
using Peter.Common.Utilities;

namespace Peter.Sdw.Data
{
   /// <summary>
   /// Data to save
   /// </summary>
   [Export (typeof (IOptionData))]
   public class SdwData : ViewModelBase, IOptionData
   {
      private bool m_SaveLogin;
      private string m_UserName;
      private string m_Password;
      private string m_SecurePassword;

      /// <summary>
      /// The name of the options.
      /// </summary>
      public const string NAME = "SDWData";

      /// <summary>
      /// Gets the name of the option data.
      /// </summary>
      public string Name
      {
         get { return NAME; }
      }

      /// <summary>
      /// Gets or Sets the user name.
      /// </summary>
      public string UserName
      {
         get { return this.m_UserName; }
         set
         {
            this.m_UserName = value;
            this.RaisePropertyChanged ("UserName");
         }
      }

      /// <summary>
      /// Gets or Sets the system should save the user login data.
      /// </summary>
      public bool SaveLogin
      {
         get { return this.m_SaveLogin; }
         set
         {
            if (this.m_SaveLogin != value)
            {
               this.m_SaveLogin = value;
               this.RaisePropertyChanged ("SaveLogin");
               if (this.m_SaveLogin)
               {
                  this.Password = "";
               }
            }
         }
      }

      /// <summary>
      /// Gets or Sets the user's password.
      /// </summary>
      [XmlIgnore]
      public string Password
      {
         get { return this.m_Password; }
         set
         {
            if (this.m_Password != value)
            {
               this.m_Password = value;
               this.RaisePropertyChanged ("Password");
               this.SecurePassword = !string.IsNullOrEmpty (this.m_Password) 
                  ? Crypto.EncryptStringAes (this.m_Password, "IFOODOAKANFRJDKV") 
                  : string.Empty;
            }
         }
      }

      /// <summary>
      /// Gets or Sets the secured password.
      /// </summary>
      public string SecurePassword
      {
         get { return this.m_SecurePassword; }
         set
         {
            if (this.m_SecurePassword != value)
            {
               this.m_SecurePassword = value;
               this.RaisePropertyChanged ("SecurePassword");
               this.Password = !string.IsNullOrEmpty (this.m_SecurePassword) 
                  ? Crypto.DecryptStringAes (this.m_SecurePassword, "IFOODOAKANFRJDKV") 
                  : string.Empty;
            }
         }
      }

      /// <summary>
      /// Loads the data from xml.
      /// </summary>
      /// <param name="xmlToLoad">Xml string to load.</param>
      public void LoadFromXml (string xmlToLoad)
      {
         SdwData loadedData = null;
         try
         {
            loadedData = Serialization.DeserializeFromString<SdwData> (xmlToLoad);
         }
         catch (Exception ex)
         {
            Console.Error.Write (ex.Message);
         }
         if (loadedData != null)
            this.CopyPropertiesFrom (loadedData);
      }

      /// <summary>
      /// Saves any data to xml.
      /// </summary>
      /// <returns>The data to save in Xml format.</returns>
      public string SaveToXml ()
      {
         return this.Serialize();
      }
   }
}
